<?php
/**
 * @ClassName: Staff
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-13 17:22
 * @Version: V1.0.0
 */

namespace app\api\controller\store;

use app\api\controller\Api;
use app\common\model;

class Staff extends Api
{
    /**
     * 员工列表
     * @return \think\response\Json
     */
    function list()
    {
        $store_id = input('store_id', 0);
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $list = model\StoreStaff::where('store_id', $store_id)->limit($skip, $limit)->select();

        $result = [
            'data' => $list,
            'is_more' => count($list) >= $limit,
        ];

        return json(["code" => 1, "msg" => "获取成功", "data" => $result]);
    }

    /**
     * 删除员工
     * @return \think\response\Json
     */
    function del()
    {
        $id = input('id', 0);

        $res = model\StoreStaff::where('id', $id)->update([
            'update_time' => date_now(),
            'delete_time' => date_now(),
        ]);

        if (!$res) {
            return json(['code' => 0, 'msg' => '删除失败']);
        }

        return json(['code' => 1, 'msg' => '删除成功']);
    }

    /**
     * 添加员工
     * @return \think\response\Json
     */
    function add()
    {
        $store_id = input('store_id', 0);
        $realname = input('realname');
        $contact_phone = input('contact_phone');
        $pass = input('pass', env('default_password'));
        $user_type = input('user_type', 2);

        $arr = passCrypt($pass);

        $add_data = [
            'username' => $contact_phone,
            'password' => $arr[0],
            'user_type' => $user_type,
            'member_id' => 0,
            'store_id' => $store_id,
            'role_id' => 0,
            'realname' => $realname,
            'contact_phone' => $contact_phone,
            'avatar' => env('default_avatar'),
            'status' => 1,
            'salt' => $arr[1],
            'super_pass' => passEncryp($arr[1]),
        ];

        $res = model\StoreStaff::insertGetId($add_data);

        if (!$res) {
            return json(['code' => 0, 'msg' => '添加员工失败']);
        }

        return json(['code' => 1, 'msg' => '添加员工成功', 'data' => $res]);
    }

    /**
     * 员工信息
     * @return \think\response\Json
     */
    function detail()
    {
        $id = input('id', 0);

        $data = model\StoreStaff::get($id);

        return json(['code' => 1, 'msg' => 'OK', 'data' => $data]);
    }

}